import time
import os
import xlwt  # 负责写excel
import numpy as np

from common.log.log import log

time = time.time()


def generateExcel(matrix):
    if not os.path.exists('data.xlsx'):
        matrix = np.array(matrix)
        filename = xlwt.Workbook()  # 创建工作簿
        sheet1 = filename.add_sheet(u'sheet1', cell_overwrite_ok=True)  # 创建sheet
    else:
        filename = 'data.xlsx'
        sheet1 = filename.add_sheet(u'sheet1', cell_overwrite_ok=True)  # 创建sheet
    [h, l] = matrix.shape  # h为行数，l为列数
    for i in range(h):
        for j in range(l):
            sheet1.write(i, j, str(matrix[i, j]))

    filename.save(f'data_{time}.xlsx')  # 保存到当前工作目录
    log.info(f'保存文件为：data_{time}.xlsx')


if __name__ == '__main__':
    matrix = [
        [2, 4, 4, 2, 3, 3, 0],
        [2, 2, 3, 2, 1, 2, 1],
        [2, 3, 3, 2, 2, 3, 1],
        [2, 2, 3, 2, 5, 2, 1],
        [2, 3, 4, 3, 2, 2, 0],
        [1, 5, 4, 2, 2, 1, 0],
        [2, 5, 4, 4, 4, 1, 0],
        [2, 4, 4, 4, 4, 7, 0],
        [1, 4, 2, 2, 2, 4, 0],
        [2, 3, 3, 2, 5, 2, 0],
        [1, 2, 2, 2, 1, 3, 0],
        [2, 5, 4, 2, 5, 2, 0],
        [1, 5, 3, 4, 4, 2, 1],
        [2, 6, 2, 2, 4, 1, 0],
        [2, 4, 4, 3, 4, 2, 0],
        [2, 2, 4, 3, 5, 3, 0],
        [1, 6, 3, 3, 4, 7, 0],
        [1, 4, 4, 3, 4, 3, 0],
        [1, 6, 3, 2, 2, 1, 0],
        [2, 3, 3, 2, 4, 2, 0],
        [1, 4, 3, 4, 5, 2, 0],
        [2, 6, 3, 4, 3, 1, 0],
        [1, 2, 1, 2, 5, 2, 0],
        [1, 5, 4, 2, 4, 1, 0],
        [1, 5, 2, 1, 1, 1, 1],
    ]
    generateExcel(matrix)
